package com.ruoyi.warn.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.warn.mapper.BusWaterWarnruleConditionsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.warn.domain.BusWaterWarnruleConditions;
import com.ruoyi.warn.mapper.BusWaterWarnruleMapper;
import com.ruoyi.warn.domain.BusWaterWarnrule;
import com.ruoyi.warn.service.IBusWaterWarnruleService;
import org.springframework.util.CollectionUtils;

/**
 * 预警规则Service业务层处理
 * 
 * @author maliang
 * @date 2025-09-10
 */
@Service
public class BusWaterWarnruleServiceImpl implements IBusWaterWarnruleService 
{
    @Autowired
    private BusWaterWarnruleMapper busWaterWarnruleMapper;
    @Autowired
    private BusWaterWarnruleConditionsMapper busWaterWarnruleConditionsMapper;

    /**
     * 查询预警规则
     * 
     * @param id 预警规则主键
     * @return 预警规则
     */
    @Override
    public BusWaterWarnrule selectBusWaterWarnruleById(String id)
    {
        return busWaterWarnruleMapper.selectBusWaterWarnruleById(id);
    }

    /**
     * 查询预警规则列表
     * 
     * @param busWaterWarnrule 预警规则
     * @return 预警规则
     */
    @Override
    public List<BusWaterWarnrule> selectBusWaterWarnruleList(BusWaterWarnrule busWaterWarnrule)
    {
        List<BusWaterWarnrule> list = busWaterWarnruleMapper.selectBusWaterWarnruleList(busWaterWarnrule);
        if(!CollectionUtils.isEmpty(list)){
            for (BusWaterWarnrule waterWarnrule : list) {
                List<BusWaterWarnruleConditions> conditionsList = selectBusWaterWarnruleById(waterWarnrule.getId()).getBusWaterWarnruleConditionsList();
                waterWarnrule.setBusWaterWarnruleConditionsList(conditionsList);
            }
        }
        return list;
    }

    /**
     * 新增预警规则
     * 
     * @param busWaterWarnrule 预警规则
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBusWaterWarnrule(BusWaterWarnrule busWaterWarnrule)
    {
        int rows = busWaterWarnruleMapper.insertBusWaterWarnrule(busWaterWarnrule);
        insertBusWaterWarnruleConditions(busWaterWarnrule);
        return rows;
    }

    /**
     * 修改预警规则
     * 
     * @param busWaterWarnrule 预警规则
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBusWaterWarnrule(BusWaterWarnrule busWaterWarnrule)
    {
        busWaterWarnrule.setUpdateBy(SecurityUtils.getUsername());
        busWaterWarnrule.setUpdatetime(DateUtils.getNowDate());
        busWaterWarnrule.setUserid(SecurityUtils.getUserId());
        insertBusWaterWarnruleConditions(busWaterWarnrule);
        return busWaterWarnruleMapper.updateBusWaterWarnrule(busWaterWarnrule);
    }

    /**
     * 批量删除预警规则
     * 
     * @param ids 需要删除的预警规则主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBusWaterWarnruleByIds(String[] ids)
    {
        busWaterWarnruleMapper.deleteBusWaterWarnruleConditionsByRuleids(ids);
        return busWaterWarnruleMapper.deleteBusWaterWarnruleByIds(ids);
    }

    /**
     * 删除预警规则信息
     * 
     * @param id 预警规则主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBusWaterWarnruleById(String id)
    {
        busWaterWarnruleMapper.deleteBusWaterWarnruleConditionsByRuleid(id);
        return busWaterWarnruleMapper.deleteBusWaterWarnruleById(id);
    }

    /**
     * 新增预警规则条件信息
     * 
     * @param busWaterWarnrule 预警规则对象
     */
    public void insertBusWaterWarnruleConditions(BusWaterWarnrule busWaterWarnrule)
    {
        List<BusWaterWarnruleConditions> busWaterWarnruleConditionsList = busWaterWarnrule.getBusWaterWarnruleConditionsList();
        String id = busWaterWarnrule.getId();
        if (StringUtils.isNotNull(busWaterWarnruleConditionsList))
        {
            for (BusWaterWarnruleConditions busWaterWarnruleConditions : busWaterWarnruleConditionsList)
            {
                busWaterWarnruleConditions.setRuleid(id);
                if(busWaterWarnruleConditions.getId() != null){
                    busWaterWarnruleConditions.setUserid(SecurityUtils.getUserId());
                    busWaterWarnruleConditions.setUpdatetime(DateUtils.getNowDate());
                    busWaterWarnruleConditionsMapper.updateBusWaterWarnruleConditions(busWaterWarnruleConditions);
                }else{
                    busWaterWarnruleConditions.setCreatetime(DateUtils.getNowDate());
                    busWaterWarnruleConditions.setUserid(SecurityUtils.getUserId());
                    busWaterWarnruleConditions.setUpdatetime(DateUtils.getNowDate());
                    busWaterWarnruleConditionsMapper.insertBusWaterWarnruleConditions(busWaterWarnruleConditions);
                }
            }
        }
    }
}
